package com.vgemv.jsutilitysdk.JsNetworkManager;

import android.content.Context;
import android.content.IntentFilter;
import android.net.ConnectivityManager;

public class JsNetworkManager {
    private Context mContext;
    private static JsNetworkManager _sharedInstance;

    /**
     * 生成全局对象,并且保存context.
     * @param context
     * @return
     */
    public static JsNetworkManager generateSharedInstance(Context context) {
        if(_sharedInstance == null) {
            _sharedInstance = new JsNetworkManager(context);
        }
        return _sharedInstance;
    }
    public static JsNetworkManager sharedInstance() {
        if(_sharedInstance == null) {
//            Exception e = new Exception("sdf");
//            throw e;
        }
        return _sharedInstance;
    }

    public JsNetworkManager(Context context) {
        mContext = context;
    }

    /**
     * 开启网络状态监控
     */
    public void startNetworkMonitoring() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        mContext.registerReceiver(JsNetworkStateBroadcastReceiver.sharedInstance(), filter);
    }

    public void stopNetworkMonitoring(Context context) {
        mContext.unregisterReceiver(JsNetworkStateBroadcastReceiver.sharedInstance());
    }

    public JsIpInfoModel getCurrentIpInfo() {
        JsIpInfoModel ipInfo = JsIpManager.getCurrentIpInfo(mContext);
        return ipInfo;
    }
}
